package com.yuan.test.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuan.model.SysRole;
import com.yuan.test.Result;
import com.yuan.test.mapper.SysRoleMapper;
import com.yuan.test.service.SysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "角色管理")
@RestController
@RequestMapping("/admin/system/sysRole")
public class SysRoleController {
    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Autowired
    private SysRoleService sysRoleService;

    @ApiOperation(value = "获取全部角色列表")
    @GetMapping("/findAll")
    public Result findAll() {
        List<SysRole> roleList = sysRoleMapper.selectList(null);
        return Result.ok(roleList);
    }

    @ApiOperation(value = "角色列表分页查询")
    @GetMapping("{page}/{limit}")
    public Result page(@PathVariable("page") Long page, @PathVariable("limit") Long limit, SysRole sysRole) {
        Page<SysRole> rolePageParams = new Page<>(page, limit);
        IPage<SysRole> rolePageModel = sysRoleService.selectPage(rolePageParams, sysRole);
        return Result.ok(rolePageModel);
    }

    @ApiOperation(value = "根据id删除角色")
    @DeleteMapping("/delete/{id}")
    public Result deleteRole(@PathVariable("id") Long id) {
        // int reslut = sysRoleMapper.deleteById(id);
        boolean b = sysRoleService.removeById(id);
        return b ? Result.ok() : Result.fail();
    }

    @ApiOperation(value = "添加角色")
    @PostMapping("/add")
    public Result addRole(SysRole sysRole) {
        boolean save = sysRoleService.save(sysRole);
        return save ? Result.ok() : Result.fail();
    }

    @ApiOperation(value = "修改角色")
    @PostMapping("/update")
    public Result updateRole(SysRole sysRole) {
        boolean b = sysRoleService.updateById(sysRole);
        return b ? Result.ok() : Result.fail();
    }
}
